using PizzaMario.Domain.Abstract;
using PizzaMario.Domain.Concrete;
using PizzaMario.Domain.Repository;

[assembly: WebActivator.PreApplicationStartMethod(typeof(PizzaMario.WebApp.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(PizzaMario.WebApp.App_Start.NinjectWebCommon), "Stop")]

namespace PizzaMario.WebApp.App_Start
{
    using System;
    using System.Web;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Common;

    //Without dependency injection, a consumer component that needs a particular service in order to accomplish a task must create 
    //an instance of a class that concretely implements the dependency interface.

    //When using dependency injection, a consumer component specifies the service contract by interface, 
    //and the injector component selects an implementation on behalf of the dependent component.
    // http://stackoverflow.com/questions/130794/what-is-dependency-injection
    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper Bootstrapper = new Bootstrapper();

        //Starts the application
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            Bootstrapper.Initialize(CreateKernel);
        }
        
        //Stops the application.
        public static void Stop()
        {
            Bootstrapper.ShutDown();
        }
        
        //Creates the kernel that will manage your application.
        //Returns the created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            
            RegisterServices(kernel);
            return kernel;
        }

        //Load your modules or register your services here!
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<DatabaseFactory>().ToSelf();
            kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>();

            //dbo
            kernel.Bind<IPizzaRepository>().To<PizzaRepository>();
            kernel.Bind<IToppingRepository>().To<ToppingRepository>();

            //TODO: Plaat hieronder eventueel meerdere services!
        }        
    }
}
